package com.sdruixinggroup.mondayb2b.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.sdruixinggroup.mondayb2b.R;
import com.sdruixinggroup.mondayb2b.adapter.ClassifyTabAdapter;
import com.sdruixinggroup.mondayb2b.adapter.QuickAdapter;
import com.sdruixinggroup.mondayb2b.models.Classify;
import com.sdruixinggroup.mondayb2b.ui.fragments.BaseFragment;
import com.sdruixinggroup.mondayb2b.utils.UserInfoUtil;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 分类tab页的子页
 */
public class ClassifyTabFragment extends BaseFragment {


    @BindView(R.id.iv_title)
    ImageView ivTitle;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    private ClassifyTabAdapter adapter;
    private List<Classify.CategoriesBean.ChildBean>  data;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int getInflateLayout() {
        return R.layout.fragment_classify_tab;
    }


    @Override
    protected void setUpView(View view) {
        adapter = new ClassifyTabAdapter(getContext());
        data = new ArrayList<>();
        adapter.setData(data);

        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(),3);
        recyclerView.setLayoutManager(gridLayoutManager);
        recyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener(new QuickAdapter.OnItemClickListener<Classify.CategoriesBean.ChildBean>(){
            @Override
            public void OnItemClick(Classify.CategoriesBean.ChildBean bean, int postion) {
                String params = "productdetail"+"?goods_id="+bean.getId()+"&area_id="+ UserInfoUtil.AREA_CODE +"&latitude="+UserInfoUtil.mLatitude
                        +"&longitude="+UserInfoUtil.mLongitude;
                String pms = "linelist?"+"&latitude="+UserInfoUtil.mLatitude
                        +"&longitude="+UserInfoUtil.mLongitude+"&second_category_id="+bean.getId()+"&area_id="+ UserInfoUtil.AREA_CODE;
                toWebView(pms);
            }
        });
        Classify.CategoriesBean classifyTabView  = ( Classify.CategoriesBean)getArguments().getSerializable("been");
        update(classifyTabView);
    }




    public void update(Classify.CategoriesBean  data){

        if(null != data){
           String name = data.getName();
            tvTitle.setText(name);
            Glide.with(getContext()).load(data.getPic()).into(ivTitle);
            List<Classify.CategoriesBean.ChildBean> childBeen = data.getChild();
            if(null != childBeen){
                 adapter.reflashData(childBeen);
            }

        }
    }

    private void toWebView(String param) {
        Intent intent = new Intent(getContext(), WebActivity.class);
        intent.putExtra(WebActivity.INTENT_EXTRA_BEGIN_URL, param);
        startActivity(intent);
    }
}
